<?php

namespace Lib\Model;

class CommentModel {

    static function getListByObjectId($objectId) {
        $cache = \Lib\Registry::get('cache');
        $key = "comment_{$objectId}";
        $result = $cache->load($key, 30);
        if(!$result) {
            $sql = "
            select  
            c.id, c.parent, c.comment, c.atUpdated as date, u.id as userId, u.username
            from post_comments pc
            join `comment` c on c.id = pc.comment_id
            join user u on u.id = c.user_id
            where pc.post_id = :object_id 
            order by pc.id desc";
            $b = \Lib\Database::getConnection()->prepare($sql);
            $b->bindValue("object_id", $objectId, \PDO::PARAM_INT);
            $b->execute();
            $comments = $b->fetchAll(\PDO::FETCH_OBJ);
            $result = array();
            if ($comments) {
                foreach ($comments as $comment) {
                    $result[] = (array) $comment;
                }
            }
            $result = self::commentsTree($result);
            $cache->save($result, $key);
        }
        return $result;
    }
    
    protected static function commentsTree($comments) {
        $comm_new = array();

        self::_showTree(0, -1, $comments, $comm_new);
        foreach($comm_new as $k => &$v){
            $v['id'] = (int) $v['id'];
            $v['level'] = isset($v['level']) ? $v['level'] : 1;
        }
        return $comm_new;
    }

    protected static function _showTree($ParentID, $lvl, &$comments, &$comm_new) {
        global $lvl;
        $lvl++;
        unset($result);
        $result = array();
        foreach ($comments as $k => $v) {
            $comments[$k]['level'] = 0;
            if ($v['parent'] == $ParentID)
                $result[] = $v;
        }
        if (count($result) > 0) {
            foreach ($result as $row) {
                $row['level'] = $lvl;
                $comm_new[] = $row;
                $ID1 = $row["id"];
                self::_showTree($ID1, $lvl, $comments, $comm_new);
                $lvl--;
            }
        }
    }
    
    static function add($comment, $objectId, $userId, $parent = 0){
        try {
            $conn = \Lib\Database::getConnection();
            $conn->beginTransaction();
            $sql = "
            insert
            into comment(parent, user_id, comment, atUpdated)
            values(:parent,:user_id,:comment,:atUpdated)";
            $b = $conn->prepare($sql);
            $b->bindValue("parent", $parent, \PDO::PARAM_INT);
            $b->bindValue("user_id", $userId, \PDO::PARAM_INT);
            $b->bindValue("comment", $comment, \PDO::PARAM_STR);
            $b->bindValue("atUpdated", date("Y-m-d H:i:s"), \PDO::PARAM_STR);
            $b->execute();
            $commentId = $conn->lastInsertId();
            $sql = "
            insert
            into post_comments(post_id, comment_id)
            values(:post_id,:comment_id)";
            $b = $conn->prepare($sql);
            $b->bindValue("post_id", $objectId, \PDO::PARAM_INT);
            $b->bindValue("comment_id", $commentId, \PDO::PARAM_INT);
            $b->execute();
            $conn->commit();
            $cache = \Lib\Registry::get('cache');
            $key = "comment_{$objectId}";
            $result = $cache->delete($key);
            return $commentId;
        } catch (\Exception $e) {
            $conn->rollBack();
            return FALSE;
        }
    }
}

?>
